package com.ysd.twocrm.utils;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AddPoiUtils {

    //获取第一行
    public Integer getFirstRowNum(String path) throws IOException {
         //获取工作簿
        XSSFWorkbook workbook=new XSSFWorkbook(path);
        XSSFSheet sheet =workbook.getSheetAt(0);
        int j=sheet.getFirstRowNum();//从第一行开始
        return j;
    }
    //最后一行
    public Integer getLastRomNum(String path) throws IOException {
          //获取工作簿
        XSSFWorkbook workbook=new XSSFWorkbook(path);
        XSSFSheet sheet=workbook.getSheetAt(0);
        int Lastrownum=sheet.getLastRowNum();
        return Lastrownum;
    }
    /*
     * @Description 调用文件里的第一行和最后一行的数据，然后通过遍历的方式来读取里面的数据并且修改成string类型
     * @param path
     * @return java.util.List<java.lang.String>
     * @author liusupeng
     * @date 2020/4/22 8:33
     */
    public List<String> stest(String path) throws IOException {
       System.out.println(path+".=");
        //获取工作表哦
        List<String> slist=new ArrayList<String>();
        //获取工作簿
        XSSFWorkbook workbook=new XSSFWorkbook(path);
        XSSFSheet sheet=workbook.getSheetAt(0);
        Integer firstRownum=getFirstRowNum(path);
        Integer lastRownum=getLastRomNum(path);
         for(int i=firstRownum+1;i<=lastRownum;i++){
             XSSFRow xssfRow=sheet.getRow(i);
             if(xssfRow!=null){
                     for(Cell cell:xssfRow){
                  if(cell!=null){
                      cell.setCellType(CellType.STRING);//修改成字符串
                      String value=cell.getStringCellValue();
                      if(value!=null && value!=""){
                          slist.add(value);
                      }
                  }
                     }
             }
         }

         return slist;
    }

}
